package com.myjoje.controller.system;

import com.myjoje.base.BaseController;
import com.myjoje.bean.system.RoleBean;
import com.myjoje.bean.system.UserBean;
import com.myjoje.service.system.UserService;
import com.myjoje.util.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 用户管理接口，包含角色，权限
 */
@RestController
@RequestMapping("/system/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 获取用户分页数据
     *
     * @param sContent 根据用户名或昵称或电话号码模糊查找
     */
    @RequestMapping("/getUserPagingData")
    public Message getUserPagingData(String roleId, String deptId, String sContent,
                                     Integer currentPage, Integer pageSize) {
        Map map = getMap("status", UserBean.STATUS_NORMAL);
        map.put("roleId", roleId);
        map.put("deptId", deptId);
        map.put("sContent", sContent);
        List<UserBean> rows = userService.getUserList(currentPage, pageSize, map);
        Long total = userService.getUserCount(map);
        return Message.success("请求成功", getPagingData(rows, total));
    }

    /**
     * 添加或更新用户
     */
    @RequestMapping("/saveOrUpdateUser")
    public Object saveOrUpdateUser(UserBean bean) {

        return userService.saveOrUpdateUser(bean);
    }

    /**
     * 角色分页数据
     */
    @RequestMapping("/getRolePagingData")
    public Object getRoleList(String name, Integer currentPage, Integer pageSize) {
        Map map = getMap("name", name);
        List<RoleBean> rows = userService.getRoleList(currentPage, pageSize, map);
        Long total = userService.getRoleCount(map);
        return Message.success("请求成功", getPagingData(rows, total));
    }

    /**
     * 获取角色列表
     */
    @RequestMapping("/getAllRole")
    public Object getAllRole() {
        List<RoleBean> list = userService.getRoleList(null, null, null);
        return Message.success("请求成功", list);
    }


    /**
     * 添加或更新角色
     */
    @RequestMapping("/saveOrUpdateRole")
    public Object saveOrUpdateRole(RoleBean bean) {
        return userService.saveOrUpdateRole(bean);
    }

    /**
     * 删除用户
     */
    @RequestMapping("/deleteUser")
    public Object deleteUser(String id) {
        return userService.deleteUser(id);
    }

    /**
     * 删除用户部门
     */
    @RequestMapping("/deleteUserDept")
    public Object deleteUserDept(String userId, String removeDeptId) {
        return userService.deleteUserDept(userId, removeDeptId);
    }

    /**
     * 删除用户角色
     */
    @RequestMapping("/deleteUserRole")
    public Object deleteUserRole(String userId, String removeRoleId) {
        return userService.deleteUserRole(userId, removeRoleId);
    }


    /**
     * 移除用户部门
     * 1.超级管理员，2.系统管理员
     */
    @RequestMapping("/getUserByRole")
    public Object getUserByRole(String role) {
        return userService.getUserByRole(role);
    }


    /**
     * 获取用户菜单
     */
    @RequestMapping("/getUserMenu")
    public Object getUserMenu() {
        return userService.getUserMenu();
    }


    /**
     * 获取用户部门
     */
    @RequestMapping("/getUserDept")
    public Object getUserDept() {
        return userService.getUserDept();
    }


}
